//
//  YYJLTestController.m
//  YuanyangJinlou
//
//  Created by mac004 on 2017/1/9.
//  Copyright © 2017年 suli19910309@qq.com. All rights reserved.
//

#import "YYJLTestController.h"
#import "YYJLFunyResultController.h"
#import "YYJLAnswerSheetController.h"
@interface YYJLTestController ()
{
    UILabel * titleLb;//考试类型
    UILabel * timeLb;//考试时间
    NSMutableArray * dateSourceArr;//全部数据
    NSArray * currentArray;//当前试题数据
    UILabel * questionLb;//问题
    UILabel * optionA;//选项A
    UILabel * optionB;//选项B
    UILabel * optionC;//选项C
    UILabel * optionD;//选项D
    UIView  * blureyView;//覆盖层
    UIView  * tipView;//提示框
    
}
@end

@implementation YYJLTestController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"第二关"];
    dateSourceArr=[[NSMutableArray alloc]init];
    currentArray=[[NSArray alloc]init];
    
    [self setBackItem];
    [self creatUI];
    
}
-(void)creatUI{
    titleLb=[MyControl createLabelWithFrame:CGRectMake(12*PER_WIDTH, 64+15*PER_HEIGHT, SCREEN_WIDTH, 15*PER_HEIGHT) Font:15 Text:@"趣味答题" Color:TINK_COLOR];
    [self.view addSubview:titleLb];
    
    timeLb=[MyControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-12*PER_WIDTH-100, 60+17*PER_HEIGHT, 100, 15*PER_HEIGHT) Font:15 Text:@"00:00:00" Color:BLACK_COLOR];
    timeLb.textAlignment=NSTextAlignmentRight;
    timeLb.font=[UIFont fontWithName:@"Helvetica-Bold" size:15];
    [self.view addSubview:timeLb];
    
    
    questionLb=[MyControl createLabelWithFrame:CGRectMake(titleLb.left, titleLb.bottom+15*PER_HEIGHT, SCREEN_WIDTH-24*PER_WIDTH, 100) Font:14 Text:@"1,趣味答题趣味答题趣味答题趣味答题趣味答题,,趣味答题趣味答题趣味答题" Color:BLACK_COLOR];
 
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.lineSpacing = 4*PER_HEIGHT;
    style.alignment = NSTextAlignmentLeft;
    NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc]initWithString:questionLb.text];
    [mutStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, questionLb.text.length)];
    [mutStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, questionLb.text.length)];
    questionLb. attributedText = mutStr;
    questionLb.numberOfLines=0;
 
     float nickHeight = [mutStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-24*PER_WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height;
    
    questionLb.frame=CGRectMake(titleLb.left, titleLb.bottom+15*PER_HEIGHT, SCREEN_WIDTH-24*PER_WIDTH, nickHeight );
    
    [self.view addSubview:questionLb];
    
    UIImage * imgA =[UIImage imageNamed:@"A_nor"];//29*29
    
    //A选项
    UIButton * btnA=[MyControl createButtonWithFrame:CGRectMake( questionLb.left, questionLb.bottom+22*PER_HEIGHT,imgA.size.width*PER_WIDTH, imgA.size.width*PER_WIDTH) ImageName:@"A_nor" Target:self Action:@selector(btnClick:) Title:@""];
    btnA.tag=1;
    [self.view addSubview:btnA];
    
    optionA=[MyControl createLabelWithFrame:CGRectMake(btnA.right+15*PER_WIDTH, questionLb.bottom+30*PER_HEIGHT, SCREEN_WIDTH-(btnA.right+27*PER_WIDTH), 15*PER_HEIGHT) Font:15 Text:@"裂纹" Color:BLACK_COLOR];
    [self.view addSubview:optionA];
    
    //B选项
    optionB=[MyControl createLabelWithFrame:CGRectMake(optionA.left, optionA.bottom+36*PER_HEIGHT, optionA.width, optionA.height) Font:15 Text:@"失去灵性" Color:BLACK_COLOR];
    [self.view addSubview:optionB];
    
    UIButton * btnB=[MyControl createButtonWithFrame:CGRectMake(optionB.left-15*PER_WIDTH-imgA.size.width*PER_WIDTH,optionB.top-8*PER_HEIGHT, imgA.size.width*PER_WIDTH, imgA.size.width*PER_WIDTH) ImageName:@"B_nor" Target:self Action:@selector(btnClick:) Title:@""];
    btnB.tag=2;
    [self.view addSubview:btnB];
    
    //C选项
    optionC=[MyControl createLabelWithFrame:CGRectMake(optionB.left, optionB.bottom+36*PER_HEIGHT, optionB.width, optionB.height) Font:15 Text:@"变黑" Color:BLACK_COLOR];
    [self.view addSubview:optionC];
    
    UIButton * btnC=[MyControl createButtonWithFrame:CGRectMake(questionLb.left, optionC.top-8*PER_HEIGHT, imgA.size.width*PER_WIDTH, imgA.size.width*PER_WIDTH) ImageName:@"C_nor" Target:self Action:@selector(btnClick:) Title:@""];
    btnC.tag=3;
    [self.view addSubview:btnC];
    
    //D选项
    optionD=[MyControl createLabelWithFrame:CGRectMake(optionC.left, optionC.bottom+36*PER_HEIGHT, optionC.width, optionC.height) Font:15 Text:@"发白" Color:BLACK_COLOR];
    [self.view addSubview:optionD];
    
    
    UIButton * btnD=[MyControl createButtonWithFrame:CGRectMake(questionLb.left, optionD.top-8*PER_HEIGHT, imgA.size.width*PER_WIDTH, imgA.size.width*PER_WIDTH) ImageName:@"D_nor" Target:self Action:@selector(btnClick:) Title:@""];
    btnD.tag=4;
    [self.view addSubview:btnD];

    //交卷
    UIImageView * bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-74*PER_HEIGHT-20*PER_HEIGHT, SCREEN_WIDTH, 74*PER_HEIGHT)];
    bgImgView.image=[UIImage imageNamed:@"bg_jiaojuan"];
    [self.view addSubview:bgImgView];
    UILabel * tjLb=[MyControl createLabelWithFrame:CGRectMake(0, 0, bgImgView.width, bgImgView.height-10*PER_HEIGHT) Font:15 Text:@"交卷" Color:WHITE_COLOR];
    tjLb.textAlignment=NSTextAlignmentCenter;
    [bgImgView addSubview:tjLb];
    
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(commitTest)];
    bgImgView.userInteractionEnabled=YES;
    [bgImgView addGestureRecognizer:tap];
    //上一题
    UIButton * aboveBtn=[MyControl createButtonWithFrame:CGRectMake(questionLb.left, SCREEN_HEIGHT-29.5*PER_HEIGHT, 45*PER_WIDTH, 15*PER_HEIGHT) ImageName:@"" Target:self Action:@selector(aboveBtn) Title:@"上一题"];
    [aboveBtn setTitleColor:FEELT_COLOR forState:0];
    aboveBtn.titleLabel.font=[UIFont systemFontOfSize:15*PER_HEIGHT];
    [self.view addSubview:aboveBtn];
    //下一题
    UIButton * nextBtn=[MyControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-12*PER_WIDTH-45*PER_WIDTH, aboveBtn.top, aboveBtn.width, aboveBtn.height) ImageName:@"" Target:self Action:@selector(nextBtn) Title:@"下一题"];
    [nextBtn setTitleColor:FEELT_COLOR forState:0];
    nextBtn.titleLabel.font=[UIFont systemFontOfSize:15*PER_HEIGHT];
    [self.view addSubview:nextBtn];
    
    blureyView=[[UIView alloc]initWithFrame:self.view.bounds];
    blureyView.backgroundColor=[UIColorFromHex(0x000000) colorWithAlphaComponent:.4];
    [self.view addSubview:blureyView];
    blureyView.hidden=YES;
    [self.view sendSubviewToBack:blureyView];
    
    
}
-(void)addTipView{
    
    blureyView.hidden=NO;
    [self.view bringSubviewToFront:blureyView];
    tipView =[[UIView alloc]initWithFrame:CGRectMake(37*PER_WIDTH, 240*PER_HEIGHT, SCREEN_WIDTH-74*PER_WIDTH, 175*PER_HEIGHT)];
    tipView.layer.cornerRadius=10;
    tipView.layer.masksToBounds=YES;
    tipView.backgroundColor=[UIColor whiteColor];
    [blureyView addSubview:tipView];
    
    UILabel * tipLb=[MyControl createLabelWithFrame:CGRectMake(0, 25*PER_HEIGHT, tipView.width, 16*PER_HEIGHT) Font:16 Text:@"提示" Color:TINK_COLOR];
    tipLb.textAlignment=NSTextAlignmentCenter;
    [tipView addSubview:tipLb];
    
    UILabel * sureLb=[MyControl createLabelWithFrame:CGRectMake(0, tipLb.bottom+20*PER_HEIGHT, tipView.width, 15*PER_HEIGHT) Font:15 Text:@"确定退出当前考试?" Color:BLACK_COLOR];
    sureLb.textAlignment=NSTextAlignmentCenter;
    [tipView addSubview:sureLb];
    
    //取消按钮
    UIButton * cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame=CGRectMake(25*PER_WIDTH, tipView.height-61*PER_HEIGHT, 120*PER_WIDTH, 36*PER_WIDTH);
    cancelBtn.layer.cornerRadius=cancelBtn.height/2;
    cancelBtn.layer.borderColor=TINK_COLOR.CGColor;
    cancelBtn.layer.borderWidth=1;
    cancelBtn.titleLabel.font=[UIFont fontWithName:HSGBW3 size:16];
    cancelBtn.titleLabel.font=[UIFont systemFontOfSize:16*PER_HEIGHT];
    [cancelBtn setTitle:@"取消" forState:0];
    [cancelBtn setTitleColor:UIColorFromHex(0xa21b18) forState:0];
    [cancelBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [tipView addSubview:cancelBtn];
   
   // 确定按钮
    UIButton * sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame=CGRectMake(cancelBtn.right+11*PER_WIDTH, tipView.height-61*PER_HEIGHT, 120*PER_WIDTH, 36*PER_WIDTH);
    sureBtn.layer.cornerRadius=cancelBtn.height/2;
//    sureBtn.layer.borderColor=TINK_COLOR.CGColor;
//    sureBtn.layer.borderWidth=1;
    sureBtn.backgroundColor=TINK_COLOR;
    sureBtn.titleLabel.font=[UIFont fontWithName:HSGBW3 size:16];
    sureBtn.titleLabel.font=[UIFont systemFontOfSize:16*PER_HEIGHT];
 
    [sureBtn setTitle:@"确定" forState:0];
    if (self.testType==SimulationTest) {
        [sureBtn setTitle:@"保存并退出" forState:0];
    }
    [sureBtn setTitleColor:WHITE_COLOR forState:0];
    [sureBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [tipView addSubview:sureBtn];
    
    
}
-(void)back{
    if (self.testType==MonthlyTest) {
        [self addTipView];
    }else if (self.testType==FunyTest){
        [self.navigationController popViewControllerAnimated:YES];
    }else if (self.testType==MonthlyTest){
        [self addTipView];
    }else{
        [self addTipView];
    }
}
#pragma mark--退出考试
-(void)cancelBtn{
    [tipView removeFromSuperview];
     blureyView.hidden=YES;
    [self.view sendSubviewToBack:blureyView];
}
-(void)sureBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark---选择选项,插入到本地数据库
-(void)btnClick:(UIButton*)sender{
    
}
#pragma mark--上一题,下一题
-(void)aboveBtn{
    
}
-(void)nextBtn{
    
}
#pragma mark--提交试卷
-(void)commitTest{
    if (self.testType==FunyTest) {
        YYJLFunyResultController * result =[[YYJLFunyResultController alloc]init];
        [self.navigationController pushViewController:result animated:YES];
    }else {
        YYJLAnswerSheetController * answer=[[YYJLAnswerSheetController alloc]init];
        [self.navigationController pushViewController:answer animated:YES];
    }
  
  
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
